let callback = [];
let pending = false;
let timerFunc;

function flush(){
  while(callback.length) {
    const cb = callback.shift();
    cb();
  }
  pending = false;
}
if (Promise) {
  timerFunc = () => {
    Promise.resolve().then(flush);
  }
} else if (MutationObserver){
  // h5异步的，可以监听dom的变化，监控完毕后再更新
  let observer = new MutationObserver(flush);
  let textNode = document.createTextNode(1); // 创建文本
  observer.observer(textNode, {CharacterData: true});
  timerFunc = () => {
    textNode.textContent = 2;
  }
} else if(setImmediate) {
  timerFunc = () => {
    setImmediate(flush)
  }
} else {
  timerFunc = () => {
    setTimeout(flush)
  }
}
export function nextTick(cb){
  callback.push(cb);
  if (!pending) {
    timerFunc()
    pending = true;
  }
}